package com.bruip.ch01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/10/22
 * @Description:
 */
class Student{
    String name;
    public Student(String name){
        this.name = name;
    }
}
public class Test08_consumer {
    public static void consume(Student student, Consumer<Student> consumer){
        consumer.accept(student);
    }
    public static void main(String[] args) {
        Student student = new Student("Tom");
        /*Consumer<Student> consumer = new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println("Hello, " + student.name + "!");
            }
        };
        consume(student, consumer);*/
        Consumer<Student> c1=(s)->System.out.println("Hello, "+s.name+"!");
        consume(student, c1);
        Consumer<Student> c2=(s)->System.out.println(s.name+" is a good student.");
        consume(student, c2);
        Consumer<Student> c3=c1.andThen(c2);
        consume(student, c3);
        //应用场景Collection foreEach方法的使用
        Collection<Student> coll=new ArrayList<>();
        coll.add(new Student("Alice"));
        coll.add(new Student("Bob"));
        coll.add(new Student("Charlie"));
        Consumer<Student> c4=(s)->s.name="hello"+s.name;
        Consumer<Student> c5=(s)->System.out.println(s.name);//System.out::println打印集合中的元素也就是学生的toString方法
        coll.forEach(c4.andThen(c5));

    }
}
